package com.jintoufs.service.hotel.impl;


import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.hotel.HotelOrderDetailMapper;
import com.jintoufs.dao.hotel.HotelOrderMapper;
import com.jintoufs.dao.hotel.HotelPriceMapper;
import com.jintoufs.dao.scene.SceneOrderDetailMapper;
import com.jintoufs.dao.scene.SceneOrderMapper;
import com.jintoufs.dao.scene.ScenePriceMapper;
import com.jintoufs.dao.tour.TourRouteMapper;
import com.jintoufs.dao.tour.TourRouteOrderDetailMapper;
import com.jintoufs.dao.tour.TourRouteOrderMapper;
import com.jintoufs.dao.tour.TourRoutePriceMapper;
import com.jintoufs.domain.account.AccountDetail;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.domain.hotel.HotelOrder;
import com.jintoufs.domain.hotel.HotelOrderDetail;
import com.jintoufs.domain.hotel.HotelPrice;
import com.jintoufs.domain.scene.SceneOrder;
import com.jintoufs.domain.scene.SceneOrderDetail;
import com.jintoufs.domain.scene.ScenePrice;
import com.jintoufs.domain.tour.TourRouteOrder;
import com.jintoufs.domain.tour.TourRouteOrderDetail;
import com.jintoufs.domain.tour.TourRoutePrice;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.hotel.HotelOrderService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.service.tour.TourRouteOrderService;
import com.jintoufs.service.tour.TourRouteService;
import com.jintoufs.service.weixin.WxRefundService;
import com.jintoufs.util.date.DateUtils;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.uuid.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class HotelOrderServiceImpl implements HotelOrderService {
    @Resource
    private HotelOrderMapper hotelOrderMapper;

    @Resource
    private TourRoutePriceMapper tourRoutePriceMapper;
    @Resource
    private SceneOrderMapper sceneOrderMapper;

    @Resource
    private TourRouteOrderMapper tourRouteOrderMapper;

    @Resource
    private TourRouteOrderDetailMapper tourRouteOrderDetailMapper;

    @Resource
    private HotelOrderDetailMapper hotelOrderDetailMapper;

    @Resource
    private SceneOrderDetailMapper sceneOrderDetailMapper;

    @Resource
    private ScenePriceMapper scenePriceMapper;

    @Resource
    private WxRefundService refundService;


    @Resource
    private HotelPriceMapper hotelPriceMapper;


    @Value("${smsAppid}")
    private Integer smsAppid;
    @Value("${smsAppkey}")
    private String smsAppkey;
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    @Resource
    DictionaryService dictionaryService;

    @Override
    public int save(HotelOrder hotelOrder) {
//        if (hotelOrder.getId()!=null){
//            return hotelOrderMapper.updateByPrimaryKeySelective(hotelOrder);
//        }else{
        return hotelOrderMapper.insertSelective(hotelOrder);
//        }
    }

    @Override
    public int saveDetail(HotelOrderDetail hotelOrderDetail, String id) {
//        if (id!=null){
//            hotelOrderDetail.setId(id);
//            return hotelOrderDetailMapper.updateByPrimaryKeySelective(hotelOrderDetail);
//        }else{
        return hotelOrderDetailMapper.insertSelective(hotelOrderDetail);
//        }
    }

    @Override
    public HotelOrder updateOrder(HotelOrder hotelOrder) {
        hotelOrder.setIsNew(Constants.ORDER_IS_NEW);
        hotelOrderMapper.updateByPrimaryKeySelective(hotelOrder);
        return hotelOrder;
    }

    @Override
    public int payConfirm(String orderId) {
        HotelOrder hotelOrder = hotelOrderMapper.selectByPrimaryKey(orderId);
        hotelOrder.setStatus(Constants.ORDER_STATUS_CONFIRM);
        hotelOrder.setReadStatus(Constants.ORDER_READ_STATUS_0);
        hotelOrder.setModifyTime(new Date());
        hotelOrder.setIsNew(Constants.ORDER_IS_NEW);
        hotelOrderMapper.updateByPrimaryKeySelective(hotelOrder);

//        List<Shop> shops =  shopService.selectByParam(param);
        Map<String,Object> params = new HashMap<>();
        params.put("id",orderId);
        List<Map<String,Object>> list =  queryByParams(params);
        if (ValidUtil.isEmpty(list)){
            return 1;
        }
        Integer smsId;
        ArrayList<String> smsParams = new ArrayList<String>();

        smsId = Integer.valueOf(dictionaryService.getDicByKeyNo("HOTEL_CONFIRM_SMS").get(0).getCaption());
        if (!ValidUtil.isEmpty(list)){
            Object object;
            object = list.get(0).get("check_in_time");
            smsParams.add(object.toString().split(" ")[0]+" 下午6点");
            object = list.get(0).get("telephone");
            if (!ValidUtil.isEmpty(object)){
                String telephone = object.toString();
                refundService.sendOrderSms(telephone,smsId,smsParams);
            }

        }

        return 1;
    }

    @Override
    public List<Map<String, Object>> queryByParams(Map<String, Object> params) {
        return hotelOrderMapper.queryByParams(params);
    }

    @Override
    public List<Map<String, Object>> queryByFilters(QueryFilters filters) {
        return hotelOrderMapper.queryByFilters(filters);
    }

//    @Override
//    public String refund(final String orderId) throws Exception{
//        return refundAction.refundOrder(orderId, null);
//    }

    @Override
    public String refundConfirm(String orderId) {
        HotelOrder hotelOrder = hotelOrderMapper.selectByPrimaryKey(orderId);
        SceneOrder sceneOrder = sceneOrderMapper.selectByPrimaryKey(orderId);
        TourRouteOrder tourRouteOrder = tourRouteOrderMapper.selectByPrimaryKey(orderId);
        if (hotelOrder != null && hotelOrder.getId() != null) {

            hotelOrder.setStatus(Constants.ORDER_STATUS_ROLLED);
            hotelOrder.setIsNew(Constants.ORDER_IS_NEW);
            hotelOrderMapper.updateByPrimaryKeySelective(hotelOrder);

            List<HotelOrderDetail> hotelOrderDetails = hotelOrderDetailMapper.selectByHotelOrderId(orderId);
            for (HotelOrderDetail hotelOrderDetail:hotelOrderDetails){
                String roomId = hotelOrderDetail.getHotelRoomId();
                Date inTime = hotelOrder.getInTime();
                HotelPrice hotelPrice = new HotelPrice();

                hotelPrice.setDate(inTime);
                hotelPrice.setHotelRoomId(roomId);
                hotelPrice = hotelPriceMapper.selectByHotelPrice(hotelPrice);
                hotelPrice.setReserve(hotelPrice.getReserve() + hotelOrderDetail.getNumber());

                hotelPriceMapper.updateReserve(hotelPrice);
            }
            return "1";
        }
        if (sceneOrder != null && sceneOrder.getId() != null) {

            sceneOrder.setStatus(Constants.ORDER_STATUS_ROLLED);
            sceneOrder.setIsNew(Constants.ORDER_IS_NEW);
            sceneOrderMapper.updateByPrimaryKeySelective(sceneOrder);

            List<SceneOrderDetail> sceneOrderDetails = sceneOrderDetailMapper.selectBySceneOrderId(orderId);
            for (SceneOrderDetail sceneOrderDetail:sceneOrderDetails){

                Date inTime = sceneOrderDetail.getCreateTime();
                ScenePrice scenePrice = new ScenePrice();

                scenePrice.setDate(inTime);
                scenePrice.setSceneId(sceneOrder.getSceneId());
                scenePrice.setReserve(scenePrice.getReserve() + sceneOrderDetail.getNumber());

                scenePriceMapper.updateReserve(scenePrice);
            }
            return "2";
        }
        if (tourRouteOrder != null && tourRouteOrder.getId() != null) {

            tourRouteOrder.setStatus(Constants.ORDER_STATUS_ROLLED);
            tourRouteOrder.setIsNew(Constants.ORDER_IS_NEW);
            tourRouteOrderMapper.updateByPrimaryKeySelective(tourRouteOrder);

            List<TourRouteOrderDetail> tourRouteOrderDetails = tourRouteOrderDetailMapper.selectByTourRouteOrderId(orderId);
//            for (TourRouteOrderDetail tourRouteOrderDetail:tourRouteOrderDetails){
                Date inTime = tourRouteOrder.getStartDate();
                TourRoutePrice tourRoutePrice = new TourRoutePrice();
//                ScenePrice scenePrice = new ScenePrice();
                tourRoutePrice.setDate(inTime);
//                scenePrice.setDate(inTime);
                tourRoutePrice.setTourRouteId(tourRouteOrder.getId());
//                scenePrice.setSceneId(sceneOrder.getSceneId());
                tourRoutePrice.setReserve(tourRoutePrice.getReserve() + tourRouteOrder.getNumber());

                tourRoutePriceMapper.updateByPrimaryKeySelective(tourRoutePrice);

//            }
            return "3";
        }
        return "";
    }

    @Override
    public HotelOrder getById(String id) {
        return hotelOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Map<String, Object>> selectByParam(Map<String, Object> param) {
        return hotelOrderMapper.getByParams(param);
    }

    public String getUUID() {
        return String.valueOf(snowflakeIdWorker.nextId());
    }

    @Override
    public Map<String, List<String>> getIndexData(Integer num, String customerId) {
        Map<String, List<String>> hotelIndex = new HashMap<>();
        Date endDate = new Date();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -num);
        c.set(Calendar.DAY_OF_MONTH, 1);
        Date startDate = c.getTime();
        Map<String, Object> param = new HashMap<>();
        param.put("endDate", endDate);
        param.put("startDate", startDate);
        if (!ValidUtil.isEmpty(customerId)) {
            param.put("customerId", customerId);
        }
        hotelIndex.put("orderNumber", hotelOrderMapper.orderIndex(param));
        hotelIndex.put("orderAmount", hotelOrderMapper.orderPriceIndex(param));

        hotelIndex.put("orderDate", hotelOrderMapper.orderDateIndex(param));
        return hotelIndex;
    }

    @Override
    public Map<String, Object> getOrderPriceAndCostByOrderId(String orderId) {
        return hotelOrderMapper.getOrderPriceAndCostByOrderId(orderId);
    }

    @Override
    public List<Map<String, Object>> getAllTypeOrderCustomerInfoDetail(Map params) {
        return hotelOrderMapper.getAllTypeOrderCustomerInfoDetail(params);
    }

    @Override
    public void sendOrderSms(String mobile, int smsId, ArrayList<String> params) {
        try {
            SmsSingleSender sender = new SmsSingleSender(smsAppid, smsAppkey);
            SmsSingleSenderResult result = sender.sendWithParam("86", mobile, smsId, params, "", "", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<HotelOrder> getNoConfirm(Map<String, Object> param) {
        return hotelOrderMapper.getNoConfirm(param);
    }

    @Override
    public int updateOrderNew(Map param) {
        return hotelOrderMapper.updateOrderNew(param);
    }


}
